package com.sa.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sa.user.api.dto.RoleDTO;
import com.sa.user.dataobject.Role;
import com.sa.user.mapper.RoleMapper;
import com.sa.user.msmapper.MsRoleMapper;
import com.sa.user.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    private final RoleMapper roleMapper;
    private final MsRoleMapper msRoleMapper;

    @Override
    public List<RoleDTO> loadRoleByUserId(long userId) {
        List<Role> roles = roleMapper.loadRolesByUserId(userId);
        if (roles != null && !roles.isEmpty()) {
            return roles.stream().map(role -> {
                return msRoleMapper.toDTO(role);
            }).toList();
        }
        return new ArrayList<RoleDTO>();
    }

    @Override
    public List<RoleDTO> loadAllRoles() {
        List<Role> roles = roleMapper.loadAllRoles();
        if (roles != null && !roles.isEmpty()) {
            return roles.stream().map(role -> {
                return msRoleMapper.toDTO(role);
            }).toList();
        }
        return null;
    }
}




